package com.chisheng.caa.config;

import com.chisheng.caa.pojo.*;
import com.chisheng.caa.service.ExpiryMap;
import com.chisheng.caa.service.SingletonValueFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;

/**
 * CAA 配置类
 */
@Configuration
@EnableConfigurationProperties(CaaProperties.class)
public class CaaConfig {

    /**
     * 年份取值的最小值
     */
    public final static int MIN_YEAR = 2010;

    /**
     * 年份取值的最大值
     */
    public final static int MAX_YEAR = 2050;

    /**
     * 最小周数
     */
    public final static int MIN_WEEKS = 1;

    /**
     * 最大周数
     */
    public final static int MAX_WEEKS = 30;

    @Resource
    private CaaProperties properties;

    /**
     * 带有效有效时间的内存缓存。
     */
    @Bean
    public ExpiryMap<String, Object> expiryMapCache() {
        return new ExpiryMap<>();
    }

    /**
     * 从配置文件配置的导入设定
     */
    @Bean
    public ImportCourseSetting importCourseSetting() {
        return new ImmutableImportCourseSetting(properties.getImporting());
    }

    /**
     * 从配置文件配置的排课设定
     */
    @Bean
    public ArrangeCourseSetting arrangeCourseSetting() {
        return new ImmutableArrangeCourseSetting(properties.getExporting());
    }

    /**
     * 有效的考试类型取值
     */
    @Bean
    public SingletonValueFactory<List<String>> validExaminationTypeList() {
        return () -> properties.getConstants().getExaminationType();
    }

    /**
     * 有效的层次取值
     */
    @Bean
    public SingletonValueFactory<List<String>> validSchoolRunningLevelList() {
        return properties.getConstants()::getSchoolRunningLevel;
    }

    /**
     * 有效的课程类型取值
     */
    @Bean
    public SingletonValueFactory<List<String>> validCourseTypeList() {
        return properties.getConstants()::getCourseType;
    }

    /**
     * 有效的总体课程分类取值
     */
    @Bean
    public SingletonValueFactory<List<String>> validCourseCategoryList() {
        return properties.getConstants()::getCourseCategory;
    }

    /**
     * 有效的中文的布尔取值
     */
    @Bean
    public SingletonValueFactory<List<String>> validBooleanValueCnList() {
        return properties.getConstants()::getBooleanValueCn;
    }

}
